﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;

namespace Xceed.Silverlight.Compatibility
{
  internal static class CompatibilityUtils
  {

#if SILVERLIGHT

    public static bool IsInDesignTool
    {
      get
      {
        return System.ComponentModel.DesignerProperties.IsInDesignTool;
      }
    }

    public static ImageSource CreateImageFrom( FrameworkElement element )
    {
      return new WriteableBitmap( element, null );
    }

    public static object GetFocusedElement( DependencyObject scope )
    {
      return FocusManager.GetFocusedElement();
    }

    public static object XamlParse( string xamlString ) 
    {
      return System.Windows.Markup.XamlReader.Load( xamlString );
    }

    public static IEnumerable<UIElement> GetEnumerable(UIElementCollection collection)
    {
      return collection;
    }

    public static bool RemoveElement( UIElementCollection collection, UIElement element )
    {
      return collection.Remove( element );
    }

#else

    public static bool IsInDesignTool
    {
      get
      {
        return false;
      }
    }

    public static ImageSource CreateImageFrom( FrameworkElement element )
    {
      Point controlTopLeft = new Point();
      Point controlBottomRight = new Point( element.ActualWidth, element.ActualHeight );

      controlTopLeft = element.RenderTransform.Transform( controlTopLeft );
      controlBottomRight = element.RenderTransform.Transform( controlBottomRight );

      int left = Math.Max( 0, Convert.ToInt32( controlTopLeft.X ) );
      int top = Math.Max( 0, Convert.ToInt32( controlTopLeft.Y ) );
      int width = Convert.ToInt32( controlBottomRight.X - controlTopLeft.X );
      int height = Convert.ToInt32( controlBottomRight.Y - controlTopLeft.Y );

      if( ( width <= 0 ) || ( height <= 0 ) )
      {
        return new BitmapImage();
      }

      RenderTargetBitmap renderBitmap = new RenderTargetBitmap( width, height, 96d, 96d, PixelFormats.Pbgra32 );

      VisualBrush sourceBrush = new VisualBrush( element );
      DrawingVisual drawingVisual = new DrawingVisual();
      DrawingContext drawingContext = drawingVisual.RenderOpen();

      using( drawingContext )
      {
        drawingContext.DrawRectangle( sourceBrush, null, new Rect( 0, 0, width, height ) );
      }

      renderBitmap.Render( drawingVisual );

      return renderBitmap;
    }

    public static object GetFocusedElement( DependencyObject scope )
    {
      return FocusManager.GetFocusedElement( scope );
    }

    private delegate void BeginInvokeDelegate();

    public static DispatcherOperation BeginInvoke( this Dispatcher dispatcher, Action a )
    {
      return dispatcher.BeginInvoke( a );
    }

    public static object XamlParse( string xamlString )
    {
      return System.Windows.Markup.XamlReader.Parse( xamlString );
    }

    public static IEnumerable<UIElement> GetEnumerable( UIElementCollection collection )
    {
      return collection.Cast<UIElement>();
    }

    public static bool RemoveElement( UIElementCollection collection, UIElement element )
    {
      bool contains = collection.Contains(element);
      collection.Remove(element);
      return contains;
    }

#endif
  }
}
